﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using The_Last_Knights.Event;

namespace The_Last_Knights.Helper.Xml
{
    public class EventReader: ReaderEntity
    {
        public override object Read(XElement element, string ID, int level)
        {
            string sourceId = element.Attribute(XName.Get(@"SourceID")).Value;
            string executionFile = element.Attribute(XName.Get(@"ExecutionFile")).Value;
            string className = element.Attribute(XName.Get(@"ClassName")).Value;
            List<string>[] _preCond = new List<string>[3];
            for (int i = 0; i < 3; i++)
            {
                _preCond[i] = new List<string>();
            }


            List<string>[] _posCond = new List<string>[3];
            for (int i = 0; i < 3; i++)
            {
                _posCond[i] = new List<string>();
            }

            var defs = element.Descendants(XName.Get(@"Element"));
            foreach (var xElement in defs)
            {
                string type = xElement.Attribute(XName.Get(@"Type")).Value;
                int lev = int.Parse(xElement.Attribute(XName.Get("Level")).Value);
                if (lev == level + 1)
                    switch (type)
                    {
                        case "PreCondition":
                            _preCond = (List<string>[]) Factory.Read(xElement, null, type, lev);
                            break;
                        case "PosCondition":
                            _posCond = (List<string>[]) Factory.Read(xElement, null, type, lev);
                            break;
                    }                
            }
            return new EventPacked(_preCond, _posCond, ID, executionFile, sourceId, className);
        }
    }
}
